package grad.nxu.recruitsystem.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import grad.nxu.recruitsystem.domain.Position;
import grad.nxu.recruitsystem.service.PositionService;
import grad.nxu.recruitsystem.vo.ApiResponse;
import grad.nxu.recruitsystem.vo.PositionVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: recruitSystem
 * @Package_name: grad.nxu.recruitsystem.controller
 * @Date: 2023/3/13 13:29
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/position")
public class PositionController {
    @Autowired
    PositionService positionService;

    /**
     * 获取所有岗位信息接口
     *
     * @param pageSize   每页多少条
     * @param pageNumber 第几页，从1开始
     * @return 返回岗位信息列表
     */
    @GetMapping("/admin/all")
    public ApiResponse<List<PositionVO>> getAll(Integer pageSize, Integer pageNumber) {
        ApiResponse<List<PositionVO>> apiResponse = new ApiResponse<>();
        try {
            List<PositionVO> positionList = positionService.getAll(pageSize, pageNumber);
            apiResponse.setData(positionList).setCode(200).setSuccess(true);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器异常");
        }
        return apiResponse;
    }

    /**
     * 删除岗位信息接口
     *
     * @param id 要删除的id
     * @return 返回删除结果
     */
    @ApiOperation("删除岗位信息接口")
    @DeleteMapping("/delete")
    public ApiResponse<String> deleteById(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            positionService.removeById(id);
            apiResponse.setMessage("删除成功").setCode(200).setSuccess(true);
        } catch (Exception e) {
            apiResponse.setSuccess(false).setCode(500).setMessage("删除失败");
        }
        return apiResponse;
    }

    @ApiOperation("添加岗位")
    @PutMapping("/add")
    public ApiResponse<String> addPositionInfo(Position position) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        boolean b = positionService.insertPosition(position);
        if (b) {
            apiResponse.setMessage("添加成功").setCode(200).setSuccess(true);
        } else {
            apiResponse.setMessage("添加失败").setCode(403).setSuccess(false);
        }
        return apiResponse;
    }

    @ApiOperation("修改岗位")
    @PutMapping("/update")
    public ApiResponse<String> updatePositionInfo(Position position) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            UpdateWrapper<Position> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setEntity(position);
            positionService.update(updateWrapper);
            apiResponse.setMessage("更新岗位成功").setCode(200).setSuccess(true);
        } catch (Exception e) {
            apiResponse.setMessage("修改岗位失败").setCode(500).setSuccess(false);
        }
        return apiResponse;
    }


}
